package com.xmh.reptile.main;

import com.alibaba.fastjson.JSONObject;
import com.xmh.reptile.ReptileApplication;
import com.xmh.reptile.dao.UserQianmeiDAO;
import com.xmh.reptile.entity.UserQianmeiEntity;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@Component
@SpringBootTest(classes = ReptileApplication.class)
public class QianMeiReptile {

    @Autowired
    private UserQianmeiDAO userQianmeiDAO;

    @Test
    public void create() throws Exception {
        String url = "https://api.dreamanimation.com/api//SearchYan_OneselfSoho/GetAll";

        String paramOld = "SecretKey=EFC1259749FCDEF9F70D0A22A7CE033665DAA933B52D134200BE7F3241D816AAF749636438BEB9BC5BDF368ABA7D04EF91CA0D65A1516219&yh=BFFCFC00D77CA9DA4146D4C689593D148914C1F44F86368687481146EA7B3B8E1592C131D0B0D5AF804A12C287F1C79F2CFC74F794477753874F3F0B8A2A428AFA0CFBBAAF4903D50D653A0F6E7C2BEA4F0C61CE5B51593C128B5C0C13443CB8A0A3ACB028DEA796CD70D869508025BFD9EF3110B11C8DD928A204457329972EAF0E0B7594CAB39DA6D842FFCABBABD5&city=%E5%B9%BF%E5%B7%9E%E5%B8%82&C_Id=ZJ0000&source=8000qm.vip&lng=113.36199&lat=23.12463&versionNumber=1.1.0&A_Id=1&platform=android&IsApp=0&Name=&IsNew=0&selectcity=&selectcounty=&Index=2&StarLevel=%E7%B1%BB%E5%9E%8B";

        Map paramMap = sendGetParamToMap(paramOld);

        for (int i = 61; i < 100; i++) {
            paramMap.put("Index", i);

            String param = sendGetParamMapToString(paramMap);

            Map dataMap = JSONObject.parseObject(sendGet(url, param), Map.class);

            List dataList = (List) dataMap.get("data");

            for (Object o : dataList) {
                Map userMap = (Map) o;

                String id = (String) userMap.get("id");
                String name = (String) userMap.get("name");

                List imgList = (List) userMap.get("img");

                String jsonString = getUserInformation(id);

                Map map = JSONObject.parseObject(jsonString, Map.class);

                List data = (List) map.get("data");

                String userDetails = getUserDetails(id);

                Map userDetailMap = JSONObject.parseObject(userDetails, Map.class);

                Map userDetailDateMap = (Map) userDetailMap.get("data");

                String site = (String) userDetailDateMap.get("site");

                List imgAll = (List) userDetailDateMap.get("file");

                UserQianmeiEntity userQianmeiEntity = new UserQianmeiEntity();
                userQianmeiEntity.setUserId(id);
                userQianmeiEntity.setName(name);

                userQianmeiEntity.setAddress(site);
                userQianmeiEntity.setImgAll(JSONObject.toJSONString(imgAll));

                if (imgList.size() > 0) {
                    userQianmeiEntity.setImg1((String) imgList.get(0));
                    if (imgList.size() > 1) {
                        userQianmeiEntity.setImg2((String) imgList.get(1));
                        if (imgList.size() > 2) {
                            userQianmeiEntity.setImg3((String) imgList.get(2));
                        }
                    }
                }

                for (Object datum : data) {
                    String contact = (String) datum;
                    if (contact.contains("qq") || contact.contains("QQ")) {
                        userQianmeiEntity.setQq(contact.split(":")[1]);
                    }
                    if (contact.contains("微信")) {
                        userQianmeiEntity.setWechat(contact.split(":")[1]);
                    }
                    if (contact.contains("手机")) {
                        userQianmeiEntity.setPhone(contact.split(":")[1]);
                    }
                }
                userQianmeiDAO.add(userQianmeiEntity);

                Thread.sleep(3000);
            }
        }
    }

    /**
     * 根据用户id获取用户信息
     *
     * @param id 用户id
     * @return
     */
    private String getUserInformation(String id) {
        String url = "https://api.dreamanimation.com/api/Home/UserExamineInfo";

        String paramOld = "SecretKey=EFC1259749FCDEF9F70D0A22A7CE033665DAA933B52D134200BE7F3241D816AAF749636438BEB9BC5BDF368ABA7D04EF91CA0D65A1516219&yh=BFFCFC00D77CA9DA4146D4C689593D148914C1F44F86368687481146EA7B3B8E1592C131D0B0D5AF804A12C287F1C79F2CFC74F794477753874F3F0B8A2A428AFA0CFBBAAF4903D50D653A0F6E7C2BEA4F0C61CE5B51593C128B5C0C13443CB8A0A3ACB028DEA796CD70D869508025BFD9EF3110B11C8DD928A204457329972EAF0E0B7594CAB39DA6D842FFCABBABD5&city=%E5%B9%BF%E5%B7%9E%E5%B8%82&C_Id=ZJ0000&source=8000qm.vip&lng=113.36199&lat=23.12463&versionNumber=1.1.0&A_Id=1&platform=android&IsApp=0&Id=A42CF32FA1780F3FC5E797AA9652763DA8A477F2F214019D&T_Id=14";

        Map paramMap = sendGetParamToMap(paramOld);

        paramMap.put("Id", id);

        String param = sendGetParamMapToString(paramMap);

        return sendGet(url, param);
    }


    private String getUserDetails(String id) {
        String url = "https://api.dreamanimation.com/api/SearchYan_OneselfSoho/GetAllById";

        String paramOld = "SecretKey=EFC1259749FCDEF9F70D0A22A7CE033665DAA933B52D134200BE7F3241D816AAF749636438BEB9BC5BDF368ABA7D04EF91CA0D65A1516219&yh=BFFCFC00D77CA9DA4146D4C689593D148914C1F44F86368687481146EA7B3B8E1592C131D0B0D5AF804A12C287F1C79F2CFC74F794477753874F3F0B8A2A428AFA0CFBBAAF4903D50D653A0F6E7C2BEA4F0C61CE5B51593C128B5C0C13443CB8A0A3ACB028DEA796CD70D869508025BFD9EF3110B11C8DD928A204457329972EAF0E0B7594CAB39DA6D842FFCABBABD5&city=%E5%B9%BF%E5%B7%9E%E5%B8%82&C_Id=ZJ0000&source=8000qm.vip&lng=113.36199&lat=23.12463&versionNumber=1.1.0&A_Id=1&platform=android&IsApp=0&Id=B7A0C2AE874D837545910233FBAB188148F1AAB545F02E81&date=Sun%20Aug%2009%202020%2012%3A23%3A47%20GMT%200800%20(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)";

        Map paramMap = sendGetParamToMap(paramOld);

        paramMap.put("Id", id);

        String param = sendGetParamMapToString(paramMap);

        return sendGet(url, param);
    }

    private String sendGetParamMapToString(Map paramMap) {
        String param = "";

        for (Object key : paramMap.keySet()) {
            param += key + "=" + paramMap.get(key) + "&";
        }
        return param;
    }

    private Map sendGetParamToMap(String param) {
        Map paramMap = new HashMap();
        for (String split1 : param.split("&")) {
            String[] split2 = split1.split("=");
            if (split2.length < 2) {
                paramMap.put(split2[0], null);
            } else {
                paramMap.put(split2[0], split2[1]);
            }
        }
        return paramMap;
    }


    public static String sendGet(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

}
